package Lee_code;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author blh
 * @date 2024年11月06日 9:32
 * 长度为 K 的子数组的能量值 I
 */
public class Lee_3254_116 {
    public int[] resultsArray(int[] nums, int k) {
        int len = nums.length;
        int[] res = new int[len - k + 1];
        if (k == 1) {
            return nums;
        }
        Deque<Integer> deque = new ArrayDeque<>();
        for (int i = 0; i < nums.length; i++) {
            if (deque.isEmpty()) {
                deque.offer(nums[i]);
                if (i >= k - 1) {
                    res[i - k + 1] = -1;
                }
                continue;
            }
            if (deque.peekLast() == nums[i] - 1) {
                deque.offer(nums[i]);
            } else {
                deque.clear();
                deque.offer(nums[i]);
            }
            if (deque.size() > k) {
                deque.pollFirst();
            }
            if (i >= k - 1) {
                if (deque.size() == k) {
                    res[i - k + 1] = deque.peekLast();
                } else {
                    res[i - k + 1] = -1;
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Lee_3254_116 method = new Lee_3254_116();
        method.resultsArray(new int[]{1}, 1);
    }
}
